package Models;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.MaskFilter;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.gamehouse.eggpoacher.DataBaseAdapter;
import com.gamehouse.eggpoacher.R;
import com.gamehouse.eggpoacher.menuPlay;

//classe que lanca um novo ovo em jogo
public class Egg {
	
	private int _id ;
	private String color;
	private int points;
	private int pictureID;
	private int eggTimer;
	
	//VIEWS afectadas pelo ovo
	private ImageView imageView;
	private Thread myTimer;//timer do nivel
	private TextView myActualScore;
	private Vibrator myVibrator;
	private int maxScore;
	private Context context;
	private MediaPlayer mp;
	
	public Egg(ImageView image,TextView actualScore, Vibrator v , 
			   EggLevels eggLevel, Context ctx, MediaPlayer newMp)
	{
		this.mp = newMp;
		this.context = ctx;
		this._id = (int) eggLevel.getEggID();
		this.eggTimer = eggLevel.getTimerEgg();
		getEggProperties(ctx);//vai a base buscar o resto das variaveis
        this.imageView = image;//imageView que vai ser manipulada
	    image.setImageResource(getPictureID());//adiciona o ovo a imageView
	    image.setVisibility(View.VISIBLE);	    
		myTimer = new Thread(myThread);
		myTimer.start();//inicia tempo de vida do ovo
		this.myActualScore = actualScore;
		this.myVibrator = v;
		actualizaScoreMax(ctx);
		image.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mp.start();
				
				v.setVisibility(View.INVISIBLE);
                System.out.println(myActualScore.getText().toString());
				int number = Integer.parseInt(myActualScore.getText().toString());
				System.out.println(number + getPoints());
				int newScore = number + getPoints();
				actualizaScorePlayer(context);
				String s =  Integer.toString(newScore);
				myActualScore.setText(s);
				myVibrator.vibrate(300);				
				myTimer.stop();
				myTimer.interrupt();
				Runtime r = Runtime.getRuntime();
				r.gc();
			}
		});	
	}
	
	//thread que controla o timer do OVO
	private Runnable myThread = new Runnable(){
		public void run() {
			
						try {
							Thread.sleep(getEggTimer());
							myHandle.sendMessage(myHandle.obtainMessage());
						} catch (InterruptedException e) {
							e.printStackTrace();
						}   
		}

		//handler e executada quando acaba i timer do OVO
		Handler myHandle = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				
				imageView.setVisibility(View.INVISIBLE);
				myTimer.stop();
				myTimer.interrupt();
				
				Runtime r = Runtime.getRuntime();
				r.gc();	
			}				
		};			
	};
	
	//get a base de dados 
	public void getEggProperties(Context ctx)
	{
		DataBaseAdapter db = new DataBaseAdapter(ctx);
		db.open();
		Cursor c = db.getEggByID(getID());		
        if (c.moveToFirst())
        {
        	do {
        		setPoints(c.getInt(0));
        		setPictureID(c.getInt(1));
        		setColor(c.getString(2));
        		System.out.println(getColor() + getPoints() + getPictureID());    		
        	} while (c.moveToNext());
        	}
        c.close();
		db.close();
	}
	
	//actualiza o maxScorePossivel do nivel
	public void actualizaScoreMax(Context ctx)
	{
		int savedMaxScore = 0;
		
		DataBaseAdapter db = new DataBaseAdapter(ctx);
		db.open();
		Cursor c = db.getActualMaxScore();		
        if (c.moveToFirst())
        {
        	do {
        		savedMaxScore = c.getInt(0);
        		System.out.println("pontos maxgravdos do nivel EGG" + savedMaxScore);
        	} while (c.moveToNext());
        	}
        c.close();
		db.close();
		
		if (getPoints() > 0)
		{
			DataBaseAdapter db2 = new DataBaseAdapter(ctx);
			db.open();
			int temp = savedMaxScore + getPoints();
			System.out.println("SOMA DOS PONTOS MAXIMOS DE NIVEL" + temp);
			db.updateMaxScore(temp);
			System.out.println("pontos maxactualizados do nivel EGG" + savedMaxScore + getPoints());
			db.close();
		}
	}
	
	//actualiza o maxScorePossivel do nivel
	public void actualizaScorePlayer(Context ctx)
	{
		int savedPlayerScore = 0;
		
		DataBaseAdapter db = new DataBaseAdapter(ctx);
		db.open();
		Cursor c = db.getActualPlayerScore();		
        if (c.moveToFirst())
        {
        	do {
        		savedPlayerScore = c.getInt(0);
        	} while (c.moveToNext());
        	}
        c.close();
		db.close();
			
			DataBaseAdapter db2 = new DataBaseAdapter(ctx);
			db.open();
			db.updatePlayerScore(savedPlayerScore + getPoints());
			
			db.close();

	}
	
	// GETERS AND SETERS
	public int getID()
	{
		return _id;
	}
	
	public int getPictureID()
	{
		return pictureID;
	}
	
	public String getColor()
	{
		return color;
	}
	
	public int getPoints()
	{
		return points;
	}
	
	public void setID(int id)
	{
		this._id = id;
	}
	
	public void setPictureID(int picture)
	{
		this.pictureID = picture;
	}
	
	
	public void setColor(String color)
	{
		this.color = color;
	}
	
	public void setPoints (int points)
	{
		this.points = points;
	}

	public int getEggTimer()
	{
		return eggTimer;
	}
	
	public void setEggTimer(int EggTimer)
	{
		this.eggTimer = EggTimer;
	}
	
	

}
